package com.stx.blog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.blog.dao.ArticleDao;
import com.stx.blog.vo.ArticleVo;
import com.stx.blog.vo.BcategoryVo;
import com.stx.blog.vo.UserVo;

public class ArticleDaoImpl extends JdbcDaoSupport implements ArticleDao {

	@Override
	public List<BcategoryVo> findBcategory(UserVo uv) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="select * from  bcategory where buid=?";
		Object args[]={uv.getBuid()};
		return jt.query(sql, args, new ArticleRowMapper());
	}
	class ArticleRowMapper implements RowMapper{

		@Override
		public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
         BcategoryVo av=new BcategoryVo();
         av.setBuid(arg0.getInt("buid"));
         av.setCid(arg0.getInt("cid"));
         av.setCname(arg0.getString("cname"));
         av.setCreatetime(arg0.getTimestamp("createtime"));
         return av; 
		
		}
		
	}
	@Override
	public void addBcategory(BcategoryVo bv) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="insert into bcategory values(cid.nextval,?,?,?)";
		Object args[]={bv.getCname(),bv.getBuid(),bv.getCreatetime()};
		jt.update(sql, args);	
	}
	@Override
	public void delBcategory(BcategoryVo bv) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="delete bcategory where cid=?";
		Object args[]={bv.getCid()};
		jt.update(sql, args);
		
	}
	@Override
	public void addArticle(ArticleVo av) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="insert into blogger values(bid.nextval,?,?,?,?,?)";
		Object args[]={av.getBuid(),av.getTitle(),av.getArticle(),av.getCid(),av.getCreatetime()};
		jt.update(sql, args);	
	}
	@Override
	public List<ArticleVo> findArticle(UserVo uv) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="select * from blogger where blogger.buid=? order by  createtime desc ";
		Object args[]={uv.getBuid()};
		return jt.query(sql, args, new ArticleListRowMapper());
	}

    public class ArticleListRowMapper implements RowMapper{

		@Override
		public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
		
		
				ArticleVo av=new ArticleVo();
				av.setArticle(arg0.getString("article"));
				av.setCid(arg0.getInt("cid"));
				av.setTitle(arg0.getString("title"));
				av.setBuid(arg0.getInt("buid"));
				av.setCreatetime(arg0.getTimestamp("createtime"));
				av.setBid(arg0.getInt("bid"));
				return av;			
			
		}
    	
    }
	@Override
	public void delArticle(ArticleVo av) {
		JdbcTemplate jt=getJdbcTemplate();
		String sql="delete from blogger where bid=?";
		Object args[]={av.getBid()};
		jt.update(sql, args);	
	}
	@Override
	public void updateArticle(ArticleVo av) {
		System.out.println(av.getBid());
		JdbcTemplate jt=getJdbcTemplate();
		String sql="update blogger set title=?,article =?,cid=?,createtime=? where bid=?";
		Object args[]={av.getTitle(),av.getArticle(),av.getCid(),av.getCreatetime(),av.getBid()};
		jt.update(sql, args);	
	}
	@Override
	public ArticleVo getArticle(ArticleVo av) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql="select * from blogger where bid=? ";
		Object args[]={av.getBid()};
		List<ArticleVo> list= jt.query(sql, args, new ArticleListRowMapper());
		return list.get(0);
	}
}
